Overview
The metrics utilities provide functions for measuring ciphertext characteristics, validating homomorphic operations, and collecting performance data. These functions are primarily used for debugging, optimization, and analysis of PVAC-HFHE operations.Functions
dump_metrics
Writes ciphertext metrics to a CSV file for analysis and debugging.const PubKey &
required
The public key used for density calculations
const char *
required
String identifier for this metric entry (e.g., “after_mul”, “before_recrypt”)
const Cipher &
required
The ciphertext to measure
const Fp &
required
The actual plaintext value (for verification purposes)
Behavior
- Creates or appends to
pvac_metrics.csvin the current directory - CSV columns:
tag,edges,layers,sigma_density,value_lo,value_hi - Thread-safe with static initialization
- Silently fails if file cannot be opened
Example output
This function is intended for development and debugging. Remove calls to
dump_metrics in production code to avoid file I/O overhead.sigma_density
Calculates the density of noise in a ciphertext by measuring the proportion of set bits in the sigma vectors.const PubKey &
required
The public key (used to access
prm.m_bits)const Cipher &
required
The ciphertext to analyze
double
The density value between 0.0 and 1.0, representing the fraction of set bits across all edge sigma vectors. Returns 0.0 if the ciphertext has no edges.
Calculation
For a ciphertext with edges E₁, E₂, …, Eₙ:popcount(Eᵢ.s) is the number of set bits in the sigma bitvector of edge i.
Usage
Density monitoring is critical for determining when to trigger recryption:Density values approaching 0.5 indicate high noise levels. The PVAC-HFHE scheme typically triggers recryption when density exceeds 0.48-0.52.
sigma_shannon
Computes the Shannon entropy of byte values in the ciphertext’s sigma vectors to assess randomness quality.const Cipher &
required
The ciphertext to analyze
double
Shannon entropy in bits (0.0 to 8.0). Higher values indicate better randomness. Returns 0.0 for empty ciphertexts.
Calculation
For byte frequency distribution p₁, p₂, …, p₂₅₆:- Maximum entropy: 8.0 bits (perfectly random)
- Low entropy: < 6.0 bits (may indicate weak randomness)
Use cases
- Validating noise generation quality
- Detecting potential side-channel vulnerabilities
- Analyzing ciphertext compressibility
This function examines the raw byte representation of sigma vectors, not the mathematical field elements. It’s primarily used for cryptographic analysis rather than operational decisions.
agg_layer_gsum
Aggregates the weighted sum of edges in a specific layer, used for internal validation.const PubKey &
required
The public key containing generator powers (
powg_B)const Cipher &
required
The ciphertext to analyze
uint32_t
required
The layer ID to aggregate
std::vector<Fp>
A vector of field elements (length
X.slots) representing the aggregated values for each slot in the specified layer.Algorithm
For each edge e in layerlid:
sgn(e.ch) is +1 for positive edges, -1 for negative edges.
This function is primarily used internally by
check_mul_gsum_all for validation. It’s not typically needed in application code.check_mul_gsum_all
Verifies the correctness of a homomorphic multiplication by checking all layer products.const PubKey &
required
The public key used for computation
const Cipher &
required
First multiplicand ciphertext
const Cipher &
required
Second multiplicand ciphertext
const Cipher &
required
Product ciphertext (should equal A × B)
bool
true if the multiplication is valid across all layer combinations, false if any discrepancy is detected.Validation logic
For each pair of layers (la, lb) from A and B:- Compute the expected product layer index in C
- Aggregate the layer sums using
agg_layer_gsum - Verify that
C[lc] = A[la] × B[lb]element-wise
Use cases
- Testing multiplication correctness during development
- Debugging homomorphic operation issues
- Validating parameter choices
This is a computationally expensive validation function. Use it only during testing, not in production code paths.
Related functions
sigma_density- Defined inops/encrypt.hpp, also available here for conveniencerecrypt- Uses density metrics to determine when recryption is neededmul- Validated bycheck_mul_gsum_all